import 'package:flutter/material.dart';

import 'package:hive_flutter/adapters.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:test2/model/game.dart';
import 'package:test2/model/map.dart';
import 'package:test2/model/scene.dart';
import 'package:test2/util/PicUtil.dart';
import 'package:test2/util/platform.dart';
import 'package:test2/util/sp.dart';
import 'package:test2/view/game_list.dart';
import 'package:test2/view/map_list.dart';
import 'package:test2/view/settings.dart';
import 'package:window_manager/window_manager.dart';

void main() async {
  if (PlatformUtil.isDesktop()) {
    WidgetsFlutterBinding.ensureInitialized();
    await windowManager.ensureInitialized();

    WindowOptions windowOptions =
        const WindowOptions(minimumSize: Size(400, 600),size: Size(500, 800), center: true);
    windowManager.waitUntilReadyToShow(windowOptions, () async {
      await windowManager.show();
      await windowManager.focus();
    });
  }

  // hotkey
  WidgetsFlutterBinding.ensureInitialized();
  await hotKeyManager.unregisterAll();
  await SpUtil.init();

  // hive
  await Hive.initFlutter("maptool");
  Hive.registerAdapter(GameAdapter());
  Hive.registerAdapter(MapCAdapter());
  Hive.registerAdapter(SceneCAdapter());

  await PicUtil.initRootPath();

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int currIndex = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        fontFamily: '微软雅黑',
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueAccent),
        // useMaterial3: true
      ),
      home: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: currIndex,
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "主页"),
            BottomNavigationBarItem(icon: Icon(Icons.settings), label: "设置"),
          ],
          onTap: (i) {
            setState(() {
              currIndex = i;
            });
          },
        ),
        body: IndexedStack(
          index: currIndex,
          children: [GameList(), SettingsPage()],
        ),
      ),
    );
  }
}
